import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';

class SwiperBox extends StatelessWidget {
  final List<String> imgList; // 图片列表
  final bool autoplay; // 是否自动播放
  final double? width; // 容器宽度
  final double? height; // 容器高度

  const SwiperBox({
    super.key,
    required this.imgList,
    this.autoplay = true,
    this.width,
    this.height,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width, // 继承外部宽度
      height: height, // 继承外部高度
      child: Swiper(
        itemCount: imgList.length,
        autoplay: autoplay,
        itemBuilder: (context, index) {
          final image = imgList[index];
          if (image.startsWith('http')) {
            // 如果是网络图片
            return Image.network(
              image,
              fit: BoxFit.contain,
              loadingBuilder: (context, child, progress) {
                if (progress == null) return child;
                return const Center(child: CircularProgressIndicator());
              },
              errorBuilder: (context, error, stackTrace) {
                return const Center(child: Text('加载失败'));
              },
            );
          } else {
            // 如果是本地图片
            return Image.asset(
              image,
              fit: BoxFit.contain,
            );
          }
        },
      ),
    );
  }
}
